package modelcontroller;

import java.util.HashMap;
import java.util.Observable;
import javax.swing.DefaultListModel;

/**
 *
 * @author Mariam
 */
public class ChatModel extends Observable {

    private User localUser;
    private boolean localUserIsConnected; // State
    private DefaultListModel connectedUsers; // localUser not in the list
    private DefaultListModel conversations;
    private HashMap<User, Conversation> hashmapConv;
    private HashMap<String, User> hashmapUsers;

    public enum Val {
        STATE,
        USERSLIST,
        CONVERSATIONSLIST;
    }

    public ChatModel() {
        //this.connectedUsers = new ArrayList<User>();
        this.connectedUsers = new DefaultListModel();
        this.hashmapUsers = new HashMap<String, User>();
        this.localUserIsConnected = false;
        // this.conversations = new ArrayList<Conversation>();
        this.conversations = new DefaultListModel();
        this.hashmapConv = new HashMap<User, Conversation>();
        //this.listObservers = new ArrayList<Observer>();
    }

    public void setLocalUserIsConnected(boolean b) {
        this.localUserIsConnected = b;
        this.setChanged();
        notifyObservers(Val.STATE);
    }

    public void setLocalUser(User localUser) {
        this.localUser = localUser;
    }

    public User getLocalUser() {
        return localUser;
    }

    public boolean getLocalUserIsConnected() {
        return localUserIsConnected;
    }

    public DefaultListModel getConnectedUsers() {
        return connectedUsers;
    }

    public DefaultListModel getConversations() {
        return conversations;
    }

    public HashMap<User, Conversation> getHashmapConv() {
        return hashmapConv;
    }

    public HashMap<String, User> getHashmapUsers() {
        return hashmapUsers;
    }

    public void addUserInList(User newUser) {
        if (!hashmapUsers.containsKey(newUser.getUsername())) {
            connectedUsers.addElement(newUser);
            hashmapUsers.put(newUser.getUsername(), newUser);
            this.setChanged();
            notifyObservers(Val.USERSLIST);
            //  System.out.println(connectedUsers.toString());
        }
    }

    public void removeUserInList(User exitingUser) {
        connectedUsers.removeElement(exitingUser);
        hashmapUsers.remove(exitingUser.getUsername());
        this.setChanged();
        notifyObservers(Val.USERSLIST);
    }

    public void addConversations(Conversation c) {
        if (!conversations.contains(c)) {
            conversations.addElement(c);
            hashmapConv.put(c.getRemoteUser(), c);
            System.out.println("adding " + c);
        }
        this.setChanged();
        notifyObservers(Val.CONVERSATIONSLIST);
    }

    public Conversation createConversation(User remoteUser) {
        Conversation c = new Conversation(remoteUser);
        conversations.addElement(c);
        hashmapConv.put(remoteUser, c);
        return c;
    }

    public void removeConversation(Conversation c) {
        conversations.removeElement(c);
        hashmapConv.remove(c.getRemoteUser());
        this.setChanged();
        notifyObservers(Val.CONVERSATIONSLIST);
    }
}
